package com.jy.adscm.operation.pebase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeAbstractContent;
import com.jy.adscm.operation.pebase.bean.QueryPeAbstractContent;
import com.jy.adscm.operation.pebase.mapper.mysql.PeAbstractContentMapper;
import com.jy.adscm.operation.pebase.service.PeAbstractContentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-04-12
 */
@Service
public class PeAbstractContentServiceImpl extends ServiceImpl<PeAbstractContentMapper, PeAbstractContent> implements PeAbstractContentService {
     @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Page<PeAbstractContent> selectPeAbstractContent(Page<PeAbstractContent> page, QueryPeAbstractContent queryPeAbstractContent) throws AdscmSystemException{
        Wrapper<PeAbstractContent> wrapper =new EntityWrapper<>();
        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryPeAbstractContent.getDid(),null)){
            throw new AdscmSystemException("did不存在");
        }
        wrapper.eq("did",queryPeAbstractContent.getDid());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryPeAbstractContent.getType(),null)){
            wrapper.eq("type",queryPeAbstractContent.getType());
        }


        page=super.selectPage(page,wrapper);
        List<PeAbstractContent> peAbstractContents = page.getRecords();
        for (PeAbstractContent peAbstractContent : peAbstractContents) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(peAbstractContent.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(peAbstractContent.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser.getName(),null)){
                    peAbstractContent.setCreatorName(jyUser.getName());
                }else {
                    peAbstractContent.setCreatorName(peAbstractContent.getCreator());
                }
            }

        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }
}
